home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / gprof / gprof.info < prev    next >
Encoding:
GNU Info File  |  1996-07-15  |  42.0 KB  |  994 lines

  1. This is Info file gprof.info, produced by Makeinfo-1.55 from the input
  2. file ./gprof.texi.
  3.  
  4. START-INFO-DIR-ENTRY
  5. * gprof: (gprof).                Profiling your program's execution
  6. END-INFO-DIR-ENTRY
  7.  
  8.    This file documents the gprof profiler of the GNU system.
  9.  
  10.    Copyright (C) 1988, 1992 Free Software Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided that
  18. the entire resulting derived work is distributed under the terms of a
  19. permission notice identical to this one.
  20.  
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions.
  24.  
  25. 
  26. File: gprof.info,  Node: Top,  Next: Why,  Prev: (DIR),  Up: (DIR)
  27.  
  28. Profiling a Program: Where Does It Spend Its Time?
  29. **************************************************
  30.  
  31.    This manual describes the GNU profiler, `gprof', and how you can use
  32. it to determine which parts of a program are taking most of the
  33. execution time.  We assume that you know how to write, compile, and
  34. execute programs.  GNU `gprof' was written by Jay Fenlason.
  35.  
  36.    This manual was updated January 1993.
  37.  
  38. * Menu:
  39.  
  40. * Why::                 What profiling means, and why it is useful.
  41. * Compiling::           How to compile your program for profiling.
  42. * Executing::           How to execute your program to generate the
  43.                             profile data file `gmon.out'.
  44. * Invoking::            How to run `gprof', and how to specify
  45.                             options for it.
  46.  
  47. * Flat Profile::        The flat profile shows how much time was spent
  48.                             executing directly in each function.
  49. * Call Graph::          The call graph shows which functions called which
  50.                             others, and how much time each function used
  51.                             when its subroutine calls are included.
  52.  
  53. * Implementation::      How the profile data is recorded and written.
  54. * Sampling Error::      Statistical margins of error.
  55.                             How to accumulate data from several runs
  56.                             to make it more accurate.
  57.  
  58. * Assumptions::         Some of `gprof''s measurements are based
  59.                             on assumptions about your program
  60.                             that could be very wrong.
  61.  
  62. * Incompatibilities::   (between GNU `gprof' and Unix `gprof'.)
  63.  
  64. 
  65. File: gprof.info,  Node: Why,  Next: Compiling,  Prev: Top,  Up: Top
  66.  
  67. Why Profile
  68. ***********
  69.  
  70.    Profiling allows you to learn where your program spent its time and
  71. which functions called which other functions while it was executing.
  72. This information can show you which pieces of your program are slower
  73. than you expected, and might be candidates for rewriting to make your
  74. program execute faster.  It can also tell you which functions are being
  75. called more or less often than you expected.  This may help you spot
  76. bugs that had otherwise been unnoticed.
  77.  
  78.    Since the profiler uses information collected during the actual
  79. execution of your program, it can be used on programs that are too
  80. large or too complex to analyze by reading the source.  However, how
  81. your program is run will affect the information that shows up in the
  82. profile data.  If you don't use some feature of your program while it
  83. is being profiled, no profile information will be generated for that
  84. feature.
  85.  
  86.    Profiling has several steps:
  87.  
  88.    * You must compile and link your program with profiling enabled.
  89.      *Note Compiling::.
  90.  
  91.    * You must execute your program to generate a profile data file.
  92.      *Note Executing::.
  93.  
  94.    * You must run `gprof' to analyze the profile data.  *Note
  95.      Invoking::.
  96.  
  97.    The next three chapters explain these steps in greater detail.
  98.  
  99.    The result of the analysis is a file containing two tables, the
  100. "flat profile" and the "call graph" (plus blurbs which briefly explain
  101. the contents of these tables).
  102.  
  103.    The flat profile shows how much time your program spent in each
  104. function, and how many times that function was called.  If you simply
  105. want to know which functions burn most of the cycles, it is stated
  106. concisely here.  *Note Flat Profile::.
  107.  
  108.    The call graph shows, for each function, which functions called it,
  109. which other functions it called, and how many times.  There is also an
  110. estimate of how much time was spent in the subroutines of each
  111. function.  This can suggest places where you might try to eliminate
  112. function calls that use a lot of time.  *Note Call Graph::.
  113.  
  114. 
  115. File: gprof.info,  Node: Compiling,  Next: Executing,  Prev: Why,  Up: Top
  116.  
  117. Compiling a Program for Profiling
  118. *********************************
  119.  
  120.    The first step in generating profile information for your program is
  121. to compile and link it with profiling enabled.
  122.  
  123.    To compile a source file for profiling, specify the `-pg' option when
  124. you run the compiler.  (This is in addition to the options you normally
  125. use.)
  126.  
  127.    To link the program for profiling, if you use a compiler such as `cc'
  128. to do the linking, simply specify `-pg' in addition to your usual
  129. options.  The same option, `-pg', alters either compilation or linking
  130. to do what is necessary for profiling.  Here are examples:
  131.  
  132.      cc -g -c myprog.c utils.c -pg
  133.      cc -o myprog myprog.o utils.o -pg
  134.  
  135.    The `-pg' option also works with a command that both compiles and
  136. links:
  137.  
  138.      cc -o myprog myprog.c utils.c -g -pg
  139.  
  140.    If you run the linker `ld' directly instead of through a compiler
  141. such as `cc', you must specify the profiling startup file
  142. `/lib/gcrt0.o' as the first input file instead of the usual startup
  143. file `/lib/crt0.o'.  In addition, you would probably want to specify
  144. the profiling C library, `/usr/lib/libc_p.a', by writing `-lc_p'
  145. instead of the usual `-lc'.  This is not absolutely necessary, but
  146. doing this gives you number-of-calls information for standard library
  147. functions such as `read' and `open'.  For example:
  148.  
  149.      ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
  150.  
  151.    If you compile only some of the modules of the program with `-pg',
  152. you can still profile the program, but you won't get complete
  153. information about the modules that were compiled without `-pg'.  The
  154. only information you get for the functions in those modules is the
  155. total time spent in them; there is no record of how many times they
  156. were called, or from where.  This will not affect the flat profile
  157. (except that the `calls' field for the functions will be blank), but
  158. will greatly reduce the usefulness of the call graph.
  159.  
  160. 
  161. File: gprof.info,  Node: Executing,  Next: Invoking,  Prev: Compiling,  Up: Top
  162.  
  163. Executing the Program to Generate Profile Data
  164. **********************************************
  165.  
  166.    Once the program is compiled for profiling, you must run it in order
  167. to generate the information that `gprof' needs.  Simply run the program
  168. as usual, using the normal arguments, file names, etc.  The program
  169. should run normally, producing the same output as usual.  It will,
  170. however, run somewhat slower than normal because of the time spent
  171. collecting and the writing the profile data.
  172.  
  173.    The way you run the program--the arguments and input that you give
  174. it--may have a dramatic effect on what the profile information shows.
  175. The profile data will describe the parts of the program that were
  176. activated for the particular input you use.  For example, if the first
  177. command you give to your program is to quit, the profile data will show
  178. the time used in initialization and in cleanup, but not much else.
  179.  
  180.    You program will write the profile data into a file called `gmon.out'
  181. just before exiting.  If there is already a file called `gmon.out', its
  182. contents are overwritten.  There is currently no way to tell the
  183. program to write the profile data under a different name, but you can
  184. rename the file afterward if you are concerned that it may be
  185. overwritten.
  186.  
  187.    In order to write the `gmon.out' file properly, your program must
  188. exit normally: by returning from `main' or by calling `exit'.  Calling
  189. the low-level function `_exit' does not write the profile data, and
  190. neither does abnormal termination due to an unhandled signal.
  191.  
  192.    The `gmon.out' file is written in the program's *current working
  193. directory* at the time it exits.  This means that if your program calls
  194. `chdir', the `gmon.out' file will be left in the last directory your
  195. program `chdir''d to.  If you don't have permission to write in this
  196. directory, the file is not written.  You may get a confusing error
  197. message if this happens.  (We have not yet replaced the part of Unix
  198. responsible for this; when we do, we will make the error message
  199. comprehensible.)
  200.  
  201. 
  202. File: gprof.info,  Node: Invoking,  Next: Flat Profile,  Prev: Executing,  Up: Top
  203.  
  204. `gprof' Command Summary
  205. ***********************
  206.  
  207.    After you have a profile data file `gmon.out', you can run `gprof'
  208. to interpret the information in it.  The `gprof' program prints a flat
  209. profile and a call graph on standard output.  Typically you would
  210. redirect the output of `gprof' into a file with `>'.
  211.  
  212.    You run `gprof' like this:
  213.  
  214.      gprof OPTIONS [EXECUTABLE-FILE [PROFILE-DATA-FILES...]] [> OUTFILE]
  215.  
  216. Here square-brackets indicate optional arguments.
  217.  
  218.    If you omit the executable file name, the file `a.out' is used.  If
  219. you give no profile data file name, the file `gmon.out' is used.  If
  220. any file is not in the proper format, or if the profile data file does
  221. not appear to belong to the executable file, an error message is
  222. printed.
  223.  
  224.    You can give more than one profile data file by entering all their
  225. names after the executable file name; then the statistics in all the
  226. data files are summed together.
  227.  
  228.    The following options may be used to selectively include or exclude
  229. functions in the output:
  230.  
  231. `-a'
  232.      The `-a' option causes `gprof' to suppress the printing of
  233.      statically declared (private) functions.  (These are functions
  234.      whose names are not listed as global, and which are not visible
  235.      outside the file/function/block where they were defined.)  Time
  236.      spent in these functions, calls to/from them, etc, will all be
  237.      attributed to the function that was loaded directly before it in
  238.      the executable file.  This option affects both the flat profile
  239.      and the call graph.
  240.  
  241. `-D'
  242.      The `-D' option causes `gprof' to ignore symbols which are not
  243.      known to be functions.  This option will give more accurate
  244.      profile data on systems where it is supported (Solaris and HPUX for
  245.      example).
  246.  
  247. `-e FUNCTION_NAME'
  248.      The `-e FUNCTION' option tells `gprof' to not print information
  249.      about the function FUNCTION_NAME (and its children...) in the call
  250.      graph.  The function will still be listed as a child of any
  251.      functions that call it, but its index number will be shown as
  252.      `[not printed]'.  More than one `-e' option may be given; only one
  253.      FUNCTION_NAME may be indicated with each `-e' option.
  254.  
  255. `-E FUNCTION_NAME'
  256.      The `-E FUNCTION' option works like the `-e' option, but time
  257.      spent in the function (and children who were not called from
  258.      anywhere else), will not be used to compute the
  259.      percentages-of-time for the call graph.  More than one `-E' option
  260.      may be given; only one FUNCTION_NAME may be indicated with each
  261.      `-E' option.
  262.  
  263. `-f FUNCTION_NAME'
  264.      The `-f FUNCTION' option causes `gprof' to limit the call graph to
  265.      the function FUNCTION_NAME and its children (and their
  266.      children...).  More than one `-f' option may be given; only one
  267.      FUNCTION_NAME may be indicated with each `-f' option.
  268.  
  269. `-F FUNCTION_NAME'
  270.      The `-F FUNCTION' option works like the `-f' option, but only time
  271.      spent in the function and its children (and their children...)
  272.      will be used to determine total-time and percentages-of-time for
  273.      the call graph.  More than one `-F' option may be given; only one
  274.      FUNCTION_NAME may be indicated with each `-F' option.  The `-F'
  275.      option overrides the `-E' option.
  276.  
  277. `-k FROM... TO...'
  278.      The `-k' option allows you to delete from the profile any arcs from
  279.      routine FROM to routine TO.
  280.  
  281. `-v'
  282.      The `-v' flag causes `gprof' to print the current version number,
  283.      and then exit.
  284.  
  285. `-z'
  286.      If you give the `-z' option, `gprof' will mention all functions in
  287.      the flat profile, even those that were never called, and that had
  288.      no time spent in them.  This is useful in conjunction with the
  289.      `-c' option for discovering which routines were never called.
  290.  
  291.    The order of these options does not matter.
  292.  
  293.    Note that only one function can be specified with each `-e', `-E',
  294. `-f' or `-F' option.  To specify more than one function, use multiple
  295. options.  For example, this command:
  296.  
  297.      gprof -e boring -f foo -f bar myprogram > gprof.output
  298.  
  299. lists in the call graph all functions that were reached from either
  300. `foo' or `bar' and were not reachable from `boring'.
  301.  
  302.    There are a few other useful `gprof' options:
  303.  
  304. `-b'
  305.      If the `-b' option is given, `gprof' doesn't print the verbose
  306.      blurbs that try to explain the meaning of all of the fields in the
  307.      tables.  This is useful if you intend to print out the output, or
  308.      are tired of seeing the blurbs.
  309.  
  310. `-c'
  311.      The `-c' option causes the static call-graph of the program to be
  312.      discovered by a heuristic which examines the text space of the
  313.      object file.  Static-only parents or children are indicated with
  314.      call counts of `0'.
  315.  
  316. `-d NUM'
  317.      The `-d NUM' option specifies debugging options.
  318.  
  319. `-s'
  320.      The `-s' option causes `gprof' to summarize the information in the
  321.      profile data files it read in, and write out a profile data file
  322.      called `gmon.sum', which contains all the information from the
  323.      profile data files that `gprof' read in.  The file `gmon.sum' may
  324.      be one of the specified input files; the effect of this is to
  325.      merge the data in the other input files into `gmon.sum'.  *Note
  326.      Sampling Error::.
  327.  
  328.      Eventually you can run `gprof' again without `-s' to analyze the
  329.      cumulative data in the file `gmon.sum'.
  330.  
  331. `-T'
  332.      The `-T' option causes `gprof' to print its output in
  333.      "traditional" BSD style.
  334.  
  335. `--function-ordering'
  336.      The `--function-ordering' option causes `gprof' to print a
  337.      suggested function ordering for the program based on profiling
  338.      data.  This option suggests an ordering which may improve paging,
  339.      tlb and cache behavior for the program on systems which support
  340.      arbitrary ordering of functions in an executable.
  341.  
  342.      The exact details of how to force the linker to place functions in
  343.      a particular order is system dependent and out of the scope of this
  344.      manual.
  345.  
  346. `--file-ordering MAP_FILE'
  347.      The `--file-ordering' option causes `gprof' to print a suggested
  348.      .o link line ordering for the program based on profiling data.
  349.      This option suggests an ordering which may improve paging, tlb and
  350.      cache behavior for the program on systems which do not support
  351.      arbitrary ordering of functions in an executable.
  352.  
  353.      Use of the `-a' argument is highly recommended with this option.
  354.  
  355.      The MAP_FILE argument is a pathname to a file which provides
  356.      function name to object file mappings.  The format of the file is
  357.      similar to the output of the program `nm'.
  358.  
  359.           c-parse.o:00000000 T yyparse
  360.           c-parse.o:00000004 C yyerrflag
  361.           c-lang.o:00000000 T maybe_objc_method_name
  362.           c-lang.o:00000000 T print_lang_statistics
  363.           c-lang.o:00000000 T recognize_objc_keyword
  364.           c-decl.o:00000000 T print_lang_identifier
  365.           c-decl.o:00000000 T print_lang_type
  366.           ...
  367.  
  368.      GNU `nm' `--extern-only' `--defined-only' `-v' `--print-file-name'
  369.      can be used to create MAP_FILE.
  370.  
  371. 
  372. File: gprof.info,  Node: Flat Profile,  Next: Call Graph,  Prev: Invoking,  Up: Top
  373.  
  374. How to Understand the Flat Profile
  375. **********************************
  376.  
  377.    The "flat profile" shows the total amount of time your program spent
  378. executing each function.  Unless the `-z' option is given, functions
  379. with no apparent time spent in them, and no apparent calls to them, are
  380. not mentioned.  Note that if a function was not compiled for profiling,
  381. and didn't run long enough to show up on the program counter histogram,
  382. it will be indistinguishable from a function that was never called.
  383.  
  384.    This is part of a flat profile for a small program:
  385.  
  386.      Flat profile:
  387.      
  388.      Each sample counts as 0.01 seconds.
  389.        %   cumulative   self              self     total
  390.       time   seconds   seconds    calls  ms/call  ms/call  name
  391.       33.34      0.02     0.02     7208     0.00     0.00  open
  392.       16.67      0.03     0.01      244     0.04     0.12  offtime
  393.       16.67      0.04     0.01        8     1.25     1.25  memccpy
  394.       16.67      0.05     0.01        7     1.43     1.43  write
  395.       16.67      0.06     0.01                             mcount
  396.        0.00      0.06     0.00      236     0.00     0.00  tzset
  397.        0.00      0.06     0.00      192     0.00     0.00  tolower
  398.        0.00      0.06     0.00       47     0.00     0.00  strlen
  399.        0.00      0.06     0.00       45     0.00     0.00  strchr
  400.        0.00      0.06     0.00        1     0.00    50.00  main
  401.        0.00      0.06     0.00        1     0.00     0.00  memcpy
  402.        0.00      0.06     0.00        1     0.00    10.11  print
  403.        0.00      0.06     0.00        1     0.00     0.00  profil
  404.        0.00      0.06     0.00        1     0.00    50.00  report
  405.      ...
  406.  
  407. The functions are sorted by decreasing run-time spent in them.  The
  408. functions `mcount' and `profil' are part of the profiling aparatus and
  409. appear in every flat profile; their time gives a measure of the amount
  410. of overhead due to profiling.
  411.  
  412.    The sampling period estimates the margin of error in each of the time
  413. figures.  A time figure that is not much larger than this is not
  414. reliable.  In this example, the `self seconds' field for `mcount' might
  415. well be `0' or `0.04' in another run.  *Note Sampling Error::, for a
  416. complete discussion.
  417.  
  418.    Here is what the fields in each line mean:
  419.  
  420. `% time'
  421.      This is the percentage of the total execution time your program
  422.      spent in this function.  These should all add up to 100%.
  423.  
  424. `cumulative seconds'
  425.      This is the cumulative total number of seconds the computer spent
  426.      executing this functions, plus the time spent in all the functions
  427.      above this one in this table.
  428.  
  429. `self seconds'
  430.      This is the number of seconds accounted for by this function alone.
  431.      The flat profile listing is sorted first by this number.
  432.  
  433. `calls'
  434.      This is the total number of times the function was called.  If the
  435.      function was never called, or the number of times it was called
  436.      cannot be determined (probably because the function was not
  437.      compiled with profiling enabled), the "calls" field is blank.
  438.  
  439. `self ms/call'
  440.      This represents the average number of milliseconds spent in this
  441.      function per call, if this function is profiled.  Otherwise, this
  442.      field is blank for this function.
  443.  
  444. `total ms/call'
  445.      This represents the average number of milliseconds spent in this
  446.      function and its descendants per call, if this function is
  447.      profiled.  Otherwise, this field is blank for this function.
  448.  
  449. `name'
  450.      This is the name of the function.   The flat profile is sorted by
  451.      this field alphabetically after the "self seconds" field is sorted.
  452.  
  453. 
  454. File: gprof.info,  Node: Call Graph,  Next: Implementation,  Prev: Flat Profile,  Up: Top
  455.  
  456. How to Read the Call Graph
  457. **************************
  458.  
  459.    The "call graph" shows how much time was spent in each function and
  460. its children.  From this information, you can find functions that,
  461. while they themselves may not have used much time, called other
  462. functions that did use unusual amounts of time.
  463.  
  464.    Here is a sample call from a small program.  This call came from the
  465. same `gprof' run as the flat profile example in the previous chapter.
  466.  
  467.      granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds
  468.      
  469.      index % time    self  children    called     name
  470.                                                       <spontaneous>
  471.      [1]    100.0    0.00    0.05                 start [1]
  472.                      0.00    0.05       1/1           main [2]
  473.                      0.00    0.00       1/2           on_exit [28]
  474.                      0.00    0.00       1/1           exit [59]
  475.      -----------------------------------------------
  476.                      0.00    0.05       1/1           start [1]
  477.      [2]    100.0    0.00    0.05       1         main [2]
  478.                      0.00    0.05       1/1           report [3]
  479.      -----------------------------------------------
  480.                      0.00    0.05       1/1           main [2]
  481.      [3]    100.0    0.00    0.05       1         report [3]
  482.                      0.00    0.03       8/8           timelocal [6]
  483.                      0.00    0.01       1/1           print [9]
  484.                      0.00    0.01       9/9           fgets [12]
  485.                      0.00    0.00      12/34          strncmp <cycle 1> [40]
  486.                      0.00    0.00       8/8           lookup [20]
  487.                      0.00    0.00       1/1           fopen [21]
  488.                      0.00    0.00       8/8           chewtime [24]
  489.                      0.00    0.00       8/16          skipspace [44]
  490.      -----------------------------------------------
  491.      [4]     59.8    0.01        0.02       8+472     <cycle 2 as a whole>    [4]
  492.                      0.01        0.02     244+260         offtime <cycle 2> [7]
  493.                      0.00        0.00     236+1           tzset <cycle 2> [26]
  494.      -----------------------------------------------
  495.  
  496.    The lines full of dashes divide this table into "entries", one for
  497. each function.  Each entry has one or more lines.
  498.  
  499.    In each entry, the primary line is the one that starts with an index
  500. number in square brackets.  The end of this line says which function
  501. the entry is for.  The preceding lines in the entry describe the
  502. callers of this function and the following lines describe its
  503. subroutines (also called "children" when we speak of the call graph).
  504.  
  505.    The entries are sorted by time spent in the function and its
  506. subroutines.
  507.  
  508.    The internal profiling function `mcount' (*note Flat Profile::.) is
  509. never mentioned in the call graph.
  510.  
  511. * Menu:
  512.  
  513. * Primary::       Details of the primary line's contents.
  514. * Callers::       Details of caller-lines' contents.
  515. * Subroutines::   Details of subroutine-lines' contents.
  516. * Cycles::        When there are cycles of recursion,
  517.                    such as `a' calls `b' calls `a'...
  518.  
  519. 
  520. File: gprof.info,  Node: Primary,  Next: Callers,  Up: Call Graph
  521.  
  522. The Primary Line
  523. ================
  524.  
  525.    The "primary line" in a call graph entry is the line that describes
  526. the function which the entry is about and gives the overall statistics
  527. for this function.
  528.  
  529.    For reference, we repeat the primary line from the entry for function
  530. `report' in our main example, together with the heading line that shows
  531. the names of the fields:
  532.  
  533.      index  % time    self  children called     name
  534.      ...
  535.      [3]    100.0    0.00    0.05       1         report [3]
  536.  
  537.    Here is what the fields in the primary line mean:
  538.  
  539. `index'
  540.      Entries are numbered with consecutive integers.  Each function
  541.      therefore has an index number, which appears at the beginning of
  542.      its primary line.
  543.  
  544.      Each cross-reference to a function, as a caller or subroutine of
  545.      another, gives its index number as well as its name.  The index
  546.      number guides you if you wish to look for the entry for that
  547.      function.
  548.  
  549. `% time'
  550.      This is the percentage of the total time that was spent in this
  551.      function, including time spent in subroutines called from this
  552.      function.
  553.  
  554.      The time spent in this function is counted again for the callers of
  555.      this function.  Therefore, adding up these percentages is
  556.      meaningless.
  557.  
  558. `self'
  559.      This is the total amount of time spent in this function.  This
  560.      should be identical to the number printed in the `seconds' field
  561.      for this function in the flat profile.
  562.  
  563. `children'
  564.      This is the total amount of time spent in the subroutine calls
  565.      made by this function.  This should be equal to the sum of all the
  566.      `self' and `children' entries of the children listed directly
  567.      below this function.
  568.  
  569. `called'
  570.      This is the number of times the function was called.
  571.  
  572.      If the function called itself recursively, there are two numbers,
  573.      separated by a `+'.  The first number counts non-recursive calls,
  574.      and the second counts recursive calls.
  575.  
  576.      In the example above, the function `report' was called once from
  577.      `main'.
  578.  
  579. `name'
  580.      This is the name of the current function.  The index number is
  581.      repeated after it.
  582.  
  583.      If the function is part of a cycle of recursion, the cycle number
  584.      is printed between the function's name and the index number (*note
  585.      Cycles::.).  For example, if function `gnurr' is part of cycle
  586.      number one, and has index number twelve, its primary line would be
  587.      end like this:
  588.  
  589.           gnurr <cycle 1> [12]
  590.  
  591. 
  592. File: gprof.info,  Node: Callers,  Next: Subroutines,  Prev: Primary,  Up: Call Graph
  593.  
  594. Lines for a Function's Callers
  595. ==============================
  596.  
  597.    A function's entry has a line for each function it was called by.
  598. These lines' fields correspond to the fields of the primary line, but
  599. their meanings are different because of the difference in context.
  600.  
  601.    For reference, we repeat two lines from the entry for the function
  602. `report', the primary line and one caller-line preceding it, together
  603. with the heading line that shows the names of the fields:
  604.  
  605.      index  % time    self  children called     name
  606.      ...
  607.                      0.00    0.05       1/1           main [2]
  608.      [3]    100.0    0.00    0.05       1         report [3]
  609.  
  610.    Here are the meanings of the fields in the caller-line for `report'
  611. called from `main':
  612.  
  613. `self'
  614.      An estimate of the amount of time spent in `report' itself when it
  615.      was called from `main'.
  616.  
  617. `children'
  618.      An estimate of the amount of time spent in subroutines of `report'
  619.      when `report' was called from `main'.
  620.  
  621.      The sum of the `self' and `children' fields is an estimate of the
  622.      amount of time spent within calls to `report' from `main'.
  623.  
  624. `called'
  625.      Two numbers: the number of times `report' was called from `main',
  626.      followed by the total number of nonrecursive calls to `report' from
  627.      all its callers.
  628.  
  629. `name and index number'
  630.      The name of the caller of `report' to which this line applies,
  631.      followed by the caller's index number.
  632.  
  633.      Not all functions have entries in the call graph; some options to
  634.      `gprof' request the omission of certain functions.  When a caller
  635.      has no entry of its own, it still has caller-lines in the entries
  636.      of the functions it calls.
  637.  
  638.      If the caller is part of a recursion cycle, the cycle number is
  639.      printed between the name and the index number.
  640.  
  641.    If the identity of the callers of a function cannot be determined, a
  642. dummy caller-line is printed which has `<spontaneous>' as the "caller's
  643. name" and all other fields blank.  This can happen for signal handlers.
  644.  
  645. 
  646. File: gprof.info,  Node: Subroutines,  Next: Cycles,  Prev: Callers,  Up: Call Graph
  647.  
  648. Lines for a Function's Subroutines
  649. ==================================
  650.  
  651.    A function's entry has a line for each of its subroutines--in other
  652. words, a line for each other function that it called.  These lines'
  653. fields correspond to the fields of the primary line, but their meanings
  654. are different because of the difference in context.
  655.  
  656.    For reference, we repeat two lines from the entry for the function
  657. `main', the primary line and a line for a subroutine, together with the
  658. heading line that shows the names of the fields:
  659.  
  660.      index  % time    self  children called     name
  661.      ...
  662.      [2]    100.0    0.00    0.05       1         main [2]
  663.                      0.00    0.05       1/1           report [3]
  664.  
  665.    Here are the meanings of the fields in the subroutine-line for `main'
  666. calling `report':
  667.  
  668. `self'
  669.      An estimate of the amount of time spent directly within `report'
  670.      when `report' was called from `main'.
  671.  
  672. `children'
  673.      An estimate of the amount of time spent in subroutines of `report'
  674.      when `report' was called from `main'.
  675.  
  676.      The sum of the `self' and `children' fields is an estimate of the
  677.      total time spent in calls to `report' from `main'.
  678.  
  679. `called'
  680.      Two numbers, the number of calls to `report' from `main' followed
  681.      by the total number of nonrecursive calls to `report'.
  682.  
  683. `name'
  684.      The name of the subroutine of `main' to which this line applies,
  685.      followed by the subroutine's index number.
  686.  
  687.      If the caller is part of a recursion cycle, the cycle number is
  688.      printed between the name and the index number.
  689.  
  690. 
  691. File: gprof.info,  Node: Cycles,  Prev: Subroutines,  Up: Call Graph
  692.  
  693. How Mutually Recursive Functions Are Described
  694. ==============================================
  695.  
  696.    The graph may be complicated by the presence of "cycles of
  697. recursion" in the call graph.  A cycle exists if a function calls
  698. another function that (directly or indirectly) calls (or appears to
  699. call) the original function.  For example: if `a' calls `b', and `b'
  700. calls `a', then `a' and `b' form a cycle.
  701.  
  702.    Whenever there are call-paths both ways between a pair of functions,
  703. they belong to the same cycle.  If `a' and `b' call each other and `b'
  704. and `c' call each other, all three make one cycle.  Note that even if
  705. `b' only calls `a' if it was not called from `a', `gprof' cannot
  706. determine this, so `a' and `b' are still considered a cycle.
  707.  
  708.    The cycles are numbered with consecutive integers.  When a function
  709. belongs to a cycle, each time the function name appears in the call
  710. graph it is followed by `<cycle NUMBER>'.
  711.  
  712.    The reason cycles matter is that they make the time values in the
  713. call graph paradoxical.  The "time spent in children" of `a' should
  714. include the time spent in its subroutine `b' and in `b''s
  715. subroutines--but one of `b''s subroutines is `a'!  How much of `a''s
  716. time should be included in the children of `a', when `a' is indirectly
  717. recursive?
  718.  
  719.    The way `gprof' resolves this paradox is by creating a single entry
  720. for the cycle as a whole.  The primary line of this entry describes the
  721. total time spent directly in the functions of the cycle.  The
  722. "subroutines" of the cycle are the individual functions of the cycle,
  723. and all other functions that were called directly by them.  The
  724. "callers" of the cycle are the functions, outside the cycle, that
  725. called functions in the cycle.
  726.  
  727.    Here is an example portion of a call graph which shows a cycle
  728. containing functions `a' and `b'.  The cycle was entered by a call to
  729. `a' from `main'; both `a' and `b' called `c'.
  730.  
  731.      index  % time    self  children called     name
  732.      ----------------------------------------
  733.                       1.77        0    1/1        main [2]
  734.      [3]     91.71    1.77        0    1+5    <cycle 1 as a whole> [3]
  735.                       1.02        0    3          b <cycle 1> [4]
  736.                       0.75        0    2          a <cycle 1> [5]
  737.      ----------------------------------------
  738.                                        3          a <cycle 1> [5]
  739.      [4]     52.85    1.02        0    0      b <cycle 1> [4]
  740.                                        2          a <cycle 1> [5]
  741.                          0        0    3/6        c [6]
  742.      ----------------------------------------
  743.                       1.77        0    1/1        main [2]
  744.                                        2          b <cycle 1> [4]
  745.      [5]     38.86    0.75        0    1      a <cycle 1> [5]
  746.                                        3          b <cycle 1> [4]
  747.                          0        0    3/6        c [6]
  748.      ----------------------------------------
  749.  
  750. (The entire call graph for this program contains in addition an entry
  751. for `main', which calls `a', and an entry for `c', with callers `a' and
  752. `b'.)
  753.  
  754.      index  % time    self  children called     name
  755.                                                   <spontaneous>
  756.      [1]    100.00       0     1.93    0      start [1]
  757.                       0.16     1.77    1/1        main [2]
  758.      ----------------------------------------
  759.                       0.16     1.77    1/1        start [1]
  760.      [2]    100.00    0.16     1.77    1      main [2]
  761.                       1.77        0    1/1        a <cycle 1> [5]
  762.      ----------------------------------------
  763.                       1.77        0    1/1        main [2]
  764.      [3]     91.71    1.77        0    1+5    <cycle 1 as a whole> [3]
  765.                       1.02        0    3          b <cycle 1> [4]
  766.                       0.75        0    2          a <cycle 1> [5]
  767.                          0        0    6/6        c [6]
  768.      ----------------------------------------
  769.                                        3          a <cycle 1> [5]
  770.      [4]     52.85    1.02        0    0      b <cycle 1> [4]
  771.                                        2          a <cycle 1> [5]
  772.                          0        0    3/6        c [6]
  773.      ----------------------------------------
  774.                       1.77        0    1/1        main [2]
  775.                                        2          b <cycle 1> [4]
  776.      [5]     38.86    0.75        0    1      a <cycle 1> [5]
  777.                                        3          b <cycle 1> [4]
  778.                          0        0    3/6        c [6]
  779.      ----------------------------------------
  780.                          0        0    3/6        b <cycle 1> [4]
  781.                          0        0    3/6        a <cycle 1> [5]
  782.      [6]      0.00       0        0    6      c [6]
  783.      ----------------------------------------
  784.  
  785.    The `self' field of the cycle's primary line is the total time spent
  786. in all the functions of the cycle.  It equals the sum of the `self'
  787. fields for the individual functions in the cycle, found in the entry in
  788. the subroutine lines for these functions.
  789.  
  790.    The `children' fields of the cycle's primary line and subroutine
  791. lines count only subroutines outside the cycle.  Even though `a' calls
  792. `b', the time spent in those calls to `b' is not counted in `a''s
  793. `children' time.  Thus, we do not encounter the problem of what to do
  794. when the time in those calls to `b' includes indirect recursive calls
  795. back to `a'.
  796.  
  797.    The `children' field of a caller-line in the cycle's entry estimates
  798. the amount of time spent *in the whole cycle*, and its other
  799. subroutines, on the times when that caller called a function in the
  800. cycle.
  801.  
  802.    The `calls' field in the primary line for the cycle has two numbers:
  803. first, the number of times functions in the cycle were called by
  804. functions outside the cycle; second, the number of times they were
  805. called by functions in the cycle (including times when a function in
  806. the cycle calls itself).  This is a generalization of the usual split
  807. into nonrecursive and recursive calls.
  808.  
  809.    The `calls' field of a subroutine-line for a cycle member in the
  810. cycle's entry says how many time that function was called from
  811. functions in the cycle.  The total of all these is the second number in
  812. the primary line's `calls' field.
  813.  
  814.    In the individual entry for a function in a cycle, the other
  815. functions in the same cycle can appear as subroutines and as callers.
  816. These lines show how many times each function in the cycle called or
  817. was called from each other function in the cycle.  The `self' and
  818. `children' fields in these lines are blank because of the difficulty of
  819. defining meanings for them when recursion is going on.
  820.  
  821. 
  822. File: gprof.info,  Node: Implementation,  Next: Sampling Error,  Prev: Call Graph,  Up: Top
  823.  
  824. Implementation of Profiling
  825. ***************************
  826.  
  827.    Profiling works by changing how every function in your program is
  828. compiled so that when it is called, it will stash away some information
  829. about where it was called from.  From this, the profiler can figure out
  830. what function called it, and can count how many times it was called.
  831. This change is made by the compiler when your program is compiled with
  832. the `-pg' option.
  833.  
  834.    Profiling also involves watching your program as it runs, and
  835. keeping a histogram of where the program counter happens to be every
  836. now and then.  Typically the program counter is looked at around 100
  837. times per second of run time, but the exact frequency may vary from
  838. system to system.
  839.  
  840.    A special startup routine allocates memory for the histogram and
  841. sets up a clock signal handler to make entries in it.  Use of this
  842. special startup routine is one of the effects of using `gcc ... -pg' to
  843. link.  The startup file also includes an `exit' function which is
  844. responsible for writing the file `gmon.out'.
  845.  
  846.    Number-of-calls information for library routines is collected by
  847. using a special version of the C library.  The programs in it are the
  848. same as in the usual C library, but they were compiled with `-pg'.  If
  849. you link your program with `gcc ... -pg', it automatically uses the
  850. profiling version of the library.
  851.  
  852.    The output from `gprof' gives no indication of parts of your program
  853. that are limited by I/O or swapping bandwidth.  This is because samples
  854. of the program counter are taken at fixed intervals of run time.
  855. Therefore, the time measurements in `gprof' output say nothing about
  856. time that your program was not running.  For example, a part of the
  857. program that creates so much data that it cannot all fit in physical
  858. memory at once may run very slowly due to thrashing, but `gprof' will
  859. say it uses little time.  On the other hand, sampling by run time has
  860. the advantage that the amount of load due to other users won't directly
  861. affect the output you get.
  862.  
  863. 
  864. File: gprof.info,  Node: Sampling Error,  Next: Assumptions,  Prev: Implementation,  Up: Top
  865.  
  866. Statistical Inaccuracy of `gprof' Output
  867. ****************************************
  868.  
  869.    The run-time figures that `gprof' gives you are based on a sampling
  870. process, so they are subject to statistical inaccuracy.  If a function
  871. runs only a small amount of time, so that on the average the sampling
  872. process ought to catch that function in the act only once, there is a
  873. pretty good chance it will actually find that function zero times, or
  874. twice.
  875.  
  876.    By contrast, the number-of-calls figures are derived by counting, not
  877. sampling.  They are completely accurate and will not vary from run to
  878. run if your program is deterministic.
  879.  
  880.    The "sampling period" that is printed at the beginning of the flat
  881. profile says how often samples are taken.  The rule of thumb is that a
  882. run-time figure is accurate if it is considerably bigger than the
  883. sampling period.
  884.  
  885.    The actual amount of error is usually more than one sampling period.
  886. In fact, if a value is N times the sampling period, the *expected*
  887. error in it is the square-root of N sampling periods.  If the sampling
  888. period is 0.01 seconds and `foo''s run-time is 1 second, the expected
  889. error in `foo''s run-time is 0.1 seconds.  It is likely to vary this
  890. much *on the average* from one profiling run to the next.  (*Sometimes*
  891. it will vary more.)
  892.  
  893.    This does not mean that a small run-time figure is devoid of
  894. information.  If the program's *total* run-time is large, a small
  895. run-time for one function does tell you that that function used an
  896. insignificant fraction of the whole program's time.  Usually this means
  897. it is not worth optimizing.
  898.  
  899.    One way to get more accuracy is to give your program more (but
  900. similar) input data so it will take longer.  Another way is to combine
  901. the data from several runs, using the `-s' option of `gprof'.  Here is
  902. how:
  903.  
  904.   1. Run your program once.
  905.  
  906.   2. Issue the command `mv gmon.out gmon.sum'.
  907.  
  908.   3. Run your program again, the same as before.
  909.  
  910.   4. Merge the new data in `gmon.out' into `gmon.sum' with this command:
  911.  
  912.           gprof -s EXECUTABLE-FILE gmon.out gmon.sum
  913.  
  914.   5. Repeat the last two steps as often as you wish.
  915.  
  916.   6. Analyze the cumulative data using this command:
  917.  
  918.           gprof EXECUTABLE-FILE gmon.sum > OUTPUT-FILE
  919.  
  920. 
  921. File: gprof.info,  Node: Assumptions,  Next: Incompatibilities,  Prev: Sampling Error,  Up: Top
  922.  
  923. Estimating `children' Times Uses an Assumption
  924. **********************************************
  925.  
  926.    Some of the figures in the call graph are estimates--for example, the
  927. `children' time values and all the the time figures in caller and
  928. subroutine lines.
  929.  
  930.    There is no direct information about these measurements in the
  931. profile data itself.  Instead, `gprof' estimates them by making an
  932. assumption about your program that might or might not be true.
  933.  
  934.    The assumption made is that the average time spent in each call to
  935. any function `foo' is not correlated with who called `foo'.  If `foo'
  936. used 5 seconds in all, and 2/5 of the calls to `foo' came from `a',
  937. then `foo' contributes 2 seconds to `a''s `children' time, by
  938. assumption.
  939.  
  940.    This assumption is usually true enough, but for some programs it is
  941. far from true.  Suppose that `foo' returns very quickly when its
  942. argument is zero; suppose that `a' always passes zero as an argument,
  943. while other callers of `foo' pass other arguments.  In this program,
  944. all the time spent in `foo' is in the calls from callers other than `a'.
  945. But `gprof' has no way of knowing this; it will blindly and incorrectly
  946. charge 2 seconds of time in `foo' to the children of `a'.
  947.  
  948.    We hope some day to put more complete data into `gmon.out', so that
  949. this assumption is no longer needed, if we can figure out how.  For the
  950. nonce, the estimated figures are usually more useful than misleading.
  951.  
  952. 
  953. File: gprof.info,  Node: Incompatibilities,  Prev: Assumptions,  Up: Top
  954.  
  955. Incompatibilities with Unix `gprof'
  956. ***********************************
  957.  
  958.    GNU `gprof' and Berkeley Unix `gprof' use the same data file
  959. `gmon.out', and provide essentially the same information.  But there
  960. are a few differences.
  961.  
  962.    * For a recursive function, Unix `gprof' lists the function as a
  963.      parent and as a child, with a `calls' field that lists the number
  964.      of recursive calls.  GNU `gprof' omits these lines and puts the
  965.      number of recursive calls in the primary line.
  966.  
  967.    * When a function is suppressed from the call graph with `-e', GNU
  968.      `gprof' still lists it as a subroutine of functions that call it.
  969.  
  970.    * The blurbs, field widths, and output formats are different.  GNU
  971.      `gprof' prints blurbs after the tables, so that you can see the
  972.      tables without skipping the blurbs.
  973.  
  974.  
  975. 
  976. Tag Table:
  977. Node: Top888
  978. Node: Why2593
  979. Node: Compiling4687
  980. Node: Executing6671
  981. Node: Invoking8782
  982. Node: Flat Profile15871
  983. Node: Call Graph19557
  984. Node: Primary22796
  985. Node: Callers25329
  986. Node: Subroutines27436
  987. Node: Cycles29095
  988. Node: Implementation35859
  989. Node: Sampling Error37959
  990. Node: Assumptions40278
  991. Node: Incompatibilities41803
  992. 
  993. End Tag Table
  994.